package cn.lookup.dao.impl;

import cn.lookup.dao.RoleDao;
import cn.lookup.domain.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("roleDao")
public class RoleDaoImpl implements RoleDao {
    @Autowired
    @Qualifier("jdbcTemplate")
    private JdbcTemplate jdbcTemplate;
    //获取角色列表
    public List<Role> getRoleList() {
        String sql = "select * from sys_role";
        List<Role> queryList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Role>(Role.class));
        return queryList;
    }
    //添加角色
    public void addRole(Role role) {
        String sql = "insert into sys_role values(?,?,?)";
        jdbcTemplate.update(sql, null, role.getRoleName(), role.getRoleDesc());
    }
    //根据人员id获取角色
    public List<Role> getRolesByUserId(Long userId) {
        String sql = "select b.* from sys_user_role a left join sys_role b on a.roleId = b.id where a.userId = ?";
        List<Role> roles = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Role>(Role.class),userId);
        return roles;
    }

    public void deleteRole(Long roleId) {
        String sql = "delete from sys_role where id = ?";
        jdbcTemplate.update(sql,roleId);
    }
}
